package com.thinkgem.jeesite.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkgem.jeesite.common.web.BaseController;

public class CSRFLoginFilter extends BaseController implements Filter{
    protected Logger logger = LoggerFactory.getLogger(CSRFLoginFilter.class);
    public static final String NEW_SESSION_INDICATOR = "com.thinkgem.jeesite.common.filter.CSRFLoginFilter";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String seesionId = httpRequest.getCookies()[0].getValue();
        String clinentId = httpRequest.getParameter("ssid");
       
        
        if(clinentId!=null){
            if(seesionId.equals(clinentId)){
                chain.doFilter(request, response);
            }else{
                httpResponse.sendRedirect(null);
                
            }
        }     
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

}
